package com.prothink.threadadmindemo.controller;



public class WangdongController {

    public static int binarySearch(int[] array, int target) {
        // 左指针
        int left = 0;
        // 右指针
        int right = array.length - 1;
        // 循环查找
        while (left <= right) {
            // 计算中间索引
            int mid = left + (right - left) / 2;
            // 判断中间元素是否等于目标
            if (array[mid] == target) {
                // 找到目标，返回索引
                return mid;
            } else if (array[mid] < target) {
                // 目标元素在右半部分
                left = mid + 1;
            } else {
                // 目标元素在左半部分
                right = mid - 1;
            }
        }
        // 未找到目标，返回-1
        return -1;
    }

    public static void main(String[] args) {
        // 定义数组
        int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
        // 定义目标元素
        int target = 15;
        // 查找目标元素
        int index = binarySearch(sortedArray, target);

        if (index != -1) {
            System.out.println("Element found at index " + index);
        } else {
            System.out.println("Element not found");
        }
    }
}
